const Koa = require("koa");
const Router = require("koa-router");
const mongoose = require("mongoose");
const bodyParser = require("koa-bodyparser")
const passport = require("koa-passport")
//实例化koa
const app = new Koa();
const router = new Router();

//全局使用bodyParser
app.use(bodyParser());

//引入users.js
const users = require("./rules/api/users");

//路由
router.get('/', async ctx => {
    ctx.body = {
        msg: "hello, this is my first Koa Interfaces"
    }
})
const db = require("./config/keys").mongoUrl
//连接数据库
mongoose.connect(db, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}).then(() => {
    console.log("Mongodb Connected!");
}).catch(err => {
    console.log(err);
})
app.use(passport.initialize());
app.use(passport.session());
//回调到config文件中的passport.js
require("./config/passport")(passport);
//配置路由地址
router.use("/api/users", users)
//配置路由
app.use(router.routes()).use(router.allowedMethods())

const port = process.env.PORT || 5000;

app.listen(port, () => {
    console.log(`server started on ${port}!`);
})